define(function(require, exports, module) {
  var $ = require("$");
  require("bootstrap_commonrequire");
  require("moment");
  require("bootstrap-daterangepicker");
  require("daterangepicker.css");
  var loader = require("./loader/loader");

  /* 监听窗口大小变化，增强jquery的resize方法，可以激情dom元素 */
  require('js/core/jquery/src/jquery_resize');

  /* 添加ztree引用 */
  require("zTreeStyle.css");
  require("ztreeCore");
  require("ztreeExcheck");

  require('select2.css');
  require('select2');
  var store = require('store');
  var bootbox = require('bootbox');

  exports.init = function(param) {

    $(document).ready(function() {
      // 蒙版
      loader.loading("数据加载中，请稍后……");
      // 加载自定义模板-条件区域
      var isSystemAdmin = (store.get("userInfo").userType == -1);
      var tplattrs = {};
      tplattrs.isSystemAdmin = isSystemAdmin;

      var template = require("./imslog.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template
      });
      new Component({
        attrs : tplattrs,
        renderTo : queryAreaId
      });

      // 获取查询条件
      function getCondition() {

        var operationtype = $('#interface_name').val();
        var status = $('#status').val();
        var tablename = $('#during').val();
        var phoneNum = $('#phoneNum').val();
        var request_msg = $('#request_msg').val();
        var result_desc = $('#result_desc').val();

        var queryStr = "";
        if (operationtype != '' && operationtype != null) {
            queryStr += ("name like '%" + operationtype + "%';");
        }

        if (status != '' && status != null) {
          if(status == '0'){
            queryStr += ("result_status='" + status + "';");            
          } else {
            queryStr += ("result_status != '0';");
          }
        }

        if (tablename != '' && tablename != null) {
          queryStr += ("during >= " + tablename + ";");
        }

        if (phoneNum != '' && phoneNum != null) {
          queryStr += ("phoneNum = " + phoneNum + ";");
        }
        
        if (request_msg != '' && request_msg != null) {
        	queryStr += ("request_msg like '%" + request_msg + "%';");
        }
        
        if (result_desc != '' && result_desc != null) {
        	queryStr += ("result_desc like '%" + result_desc + "%';");
        }
        
        // 获取时间查询参数
        var daterange = daterangeCom.getPickerRange();
        if (daterange) {
          var daterange = daterange.split("/");
          var startTime = daterange[0];
          var endTime = daterange[1];
          queryStr += "create_time>='" + startTime + " 00:00:00';create_time<='" + endTime + " 23:59:59'";

        }

        return queryStr;
      }

      
      
      var dataStatus = [ {
        "id" : "0",
        "text" : "成功"
      }, {
        "id" : "-1",
        "text" : "失败"
      }];
      $("#status").select2({
        placeholder : "请选择回调状态",
        allowClear : true,
        data : dataStatus
      });

      // 初始化日期范围选择控件
      var DateRange = require('daterangeinput');
      var daterangeAttrs = {
        daterangeId : "querydatewrapper_rang",
        label : "日期"
      };
      var daterangeCom = new DateRange({
        attrs : daterangeAttrs,
        renderTo : 'querydatewrapper'
      });
      daterangeCom.setRangeToToday();

      // 加载表单模板和数据
      var Table = require("table");
      new Table({
        attrs : {
          id : "datatableid",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });

      var dtUtil = require('datatableutil');

      var datatable = $('#datatableid').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "full_numbers", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl, // 数据源
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          // 拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
            "dataType" : 'json',
            "contentType" : "application/json;charset=UTF-8",
            "type" : "GET",
            "url" : sSource,
            "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              if(json.flag == '000000'){
                // 隐藏蒙版
                loader.noloading();
                dtUtil.successCallback(json, aoData, fnCallback);                    
              } else {
                oauth2Relogin();                    
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "能力类型 ",
          "bSortable" : false,
          "mData" : "type",
          "sWidth" : "10%",
          "mRender" : function(data, type, full) {
              if (data == "RongLian")
                return "荣联通道";
              else if (data == "remind")
                  return "提醒服务";
              else if (data == "Platform")
                  return "运营平台";
              else if (data == "YonYou")
                  return "自建平台";
              else if (data == "MiCh")
                  return "名传通道";
              else if (data == "WeiWang")
                  return "微网互联通道";
              else if (data == "BaiWu")
                  return "百悟通道";
              else if (data == "TianRun")
                  return "天润融通";
              else if (data == "ChengQi")
                  return "承启通";
              else if (data == "IMS")
                  return "IMS通道";
              else 
                  return data;
            } 
        }, {
          "sTitle" : "接口名称",
          "bSortable" : false,
          "mData" : "name",
          "sWidth" : "10%",
          "mRender" : function(data, type, full) {
              return data;
          }
        }, {
          "sTitle" : "耗时 (秒)",
          "bSortable" : false,
          "mData" : "during",
         "sWidth" : "5%"
        }, {
          "sTitle" : "回调状态",
          "bSortable" : false,
          "mData" : "result_status",
          "sWidth" : "5%",
          "mRender" : function(data, type, full) {
            if (data == "0")
              return "成功";
            else
              return "失败";
          }
        }, {
          "sTitle" : "回调详情",
          "bSortable" : false,
          "mData" : "result_desc",
          "sWidth" : "25%",
          "mRender" : function(data, type, full) {
            var ret = '<div>';
            if(data.length < 10){
              ret += '<a href="javascript:void(0);" class="btn-link-1" title="查看详情">'+data+'</a>';
            }else{
              ret += '<a href="javascript:void(0);" class="btn-link-1" title="查看详情">'+data.substr(0,30)+'...</a>';
            }
            return ret += '</div>';

          }
        }, {
          "sTitle" : "请求信息",
          "bSortable" : false,
          "mData" : "request_msg",
          "sWidth" : "25%",
          "mRender" : function(data, type, full) {
            var ret = '<div>';
//            ret += '<a href="javascript:void(0);" class="btn-link-2" title="查看详情">'+data.substr(25,55)+'...</a>';
            ret += '<a href="javascript:void(0);" class="btn-link-2" title="查看详情">'+data.substr(0,20)+'...</a>';
            return ret += '</div>';

          }
        },{
          "sTitle" : "主叫号码",
          "bSortable" : false,
          "mData" : "phoneNum",
          "sWidth" : "5%"
        },{
          "sTitle" : "创建时间",
          "bSortable" : false,
          "mData" : "create_time",
          "sWidth" : "15%"
        } ],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });
      // 查看详情
      $("#datatableid tbody").on("click", "tr>td>div>a.btn-link-1", function(e) {
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = datatable.fnGetData(rowNode);
        logdetail(rowData.result_desc);
      });
      // 查看详情
      $("#datatableid tbody").on("click", "tr>td>div>a.btn-link-2", function(e) {
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = datatable.fnGetData(rowNode);
        logdetail(rowData.request_msg.replace("http://110.84.128.78:8088/", ""));
      });
      // 查看详情
      var logdetail = function(context) {
        var log_detail = require("./systemlogdetail");
        if ($("#logdetailid").length <= 0)
          $("body").append("<div id='logdetailid'></div>");
        log_detail.init(
            {
              "title" : "详细信息",
              "renderTo" : "logdetailid",
              "details": context
            });

      };
      
      // 查询按钮
      $('#btnquery').click(function() {
        // datatable.fnDraw();
        //蒙版
        loader.loading("数据加载中，请稍后……");
        datatable.fnFilter("");
        return false;
      });
      
      // 重置按钮
      $('#restbtn').click(function() {
        //蒙版
        loader.loading("数据加载中，请稍后……");
        $('#interface_name').val("");
        $('#status').select2("val", "");
        $('#during').val("");
        $('#phoneNum').val("");
        $('#request_msg').val("");
        $('#result_desc').val("");
        daterangeCom.setRangeToToday();
        datatable.fnFilter("");
        return false;
      });

    });

  };
});
